/**
 * 生成分页组件
 *
 * @param {Object} options 配置对象
 * @param {number} options.totalCount 总数据量
 * @param {number} options.pageCount 总页数
 * @param {number} options.perPage 每页数据量
 * @param {number} options.page 当前页
 * @param {string} containerSelector 容器选择器，生成的分页组件将被插入到该容器中
 */
function generatePagination(options, containerSelector, activePage) {
    let cPage = 1;
    const {totalCount, pageCount, perPage, page} = options;

    // 获取容器元素
    const container = document.querySelector(containerSelector);
    container.innerHTML = ''; // 清空容器内容

    // 创建分页容器
    const pagination = document.createElement('nav');
    pagination.setAttribute('aria-label', 'Page navigation');
    pagination.classList.add('pagination', 'pagination-lg', 'justify-content-center');

    // 计算页码范围（每次显示 10 个页码）
    const startPage = Math.floor((page - 1) / 10) * 10 + 1;
    const endPage = Math.min(startPage + 9, pageCount);

    // 上一页按钮
    const prevButton = document.createElement('li');
    prevButton.classList.add('page-item');
    prevButton.classList.toggle('disabled', page === 1);
    const prevLink = document.createElement('a');
    prevLink.classList.add('page-link');
    prevLink.href = '#';
    prevLink.innerHTML = '<<';
    prevLink.addEventListener('click', function (e) {
        e.preventDefault();
        if (page > 1) {
            const newPage = Math.max(1, startPage - 10);
            generatePagination({...options, page: newPage}, containerSelector, newPage);
        }
    });
    prevButton.appendChild(prevLink);
    pagination.appendChild(prevButton);

    // 页码按钮
    for (let i = startPage; i <= endPage; i++) {
        const pageItem = document.createElement('li');
        pageItem.classList.add('page-item');
        if (''+i == ''+page) {
            pageItem.classList.add('active');
        }
        const pageLink = document.createElement('a');
        pageLink.classList.add('page-link');
        pageLink.href = '#';
        pageLink.textContent = i;
        pageLink.addEventListener('click', function (e) {
            e.preventDefault();
            turnPage(pageLink);
            generatePagination({...options, page: ''+i}, containerSelector);
        });
        pageItem.appendChild(pageLink);
        pagination.appendChild(pageItem);


    }

    // 下一页按钮
    const nextButton = document.createElement('li');
    nextButton.classList.add('page-item');
    nextButton.classList.toggle('disabled', page === pageCount);
    const nextLink = document.createElement('a');
    nextLink.classList.add('page-link');
    nextLink.href = '#';
    nextLink.innerHTML = '>>';
    nextLink.addEventListener('click', function (e) {
        e.preventDefault();
        if (page < pageCount) {
            const newPage = Math.min(pageCount, startPage + 10);
            generatePagination({...options, page: newPage}, containerSelector, newPage);
        }
    });
    nextButton.appendChild(nextLink);
    pagination.appendChild(nextButton);

    // 将分页组件添加到指定容器中
    container.appendChild(pagination);

    if (activePage) {
        turnPage(null,activePage);
    }
}
